package com.ixuestar.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ixuestar.dao.IxuestarParentsMapper;
import com.ixuestar.entity.ExaminationInfo;
import com.ixuestar.entity.ExaminationQuestions;
import com.ixuestar.entity.ExaminationResults;
import com.ixuestar.entity.Examinationsubject;
import com.ixuestar.entity.MemberAndParents;
import com.ixuestar.entity.MemberStudent;
import com.ixuestar.entity.MembershipInfo;
import com.ixuestar.entity.MembershipVipInfo;
import com.ixuestar.entity.Membershiparents;
import com.ixuestar.entity.QuestionScore;
import com.ixuestar.entity.School;
import com.ixuestar.entity.SchoolClass;
import com.ixuestar.entity.SchoolGrade;
import com.ixuestar.entity.SchoolStudent;
import com.ixuestar.entity.Subject;
import com.ixuestar.entity.Temp;
import com.ixuestar.service.IxuestarParentsService;

@Service("/ixuestarParentsService")
@Transactional
public class IxuestarParentsServiceImpl implements IxuestarParentsService {
	
	@Autowired
	public IxuestarParentsMapper ixuestarParentsMapper;
	
	@Override
	public JSONObject bindingInfoByTelephone(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		int code = 0;
		try {
			String ixmNo = jsonObject.getString("ixmNo");
			//通过会员号获取ixmId
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ixmNo");
			hashMap.put("lineValue", ixmNo);
			MembershipInfo membershipInfo = ixuestarParentsMapper.getMembershipInfo(hashMap);
			if (membershipInfo!=null) {
				String ixmId = membershipInfo.getIxmId();
				//绑定信息
				//首先写入家长信息
				Membershiparents membershiparents = new Membershiparents();
				//创建信息id
				String ixmpOpenid = jsonObject.getString("ixmpOpenid");
				String ixmpTelephone = jsonObject.getString("ixmpTelephone");
				hashMap.put("lineKey", "ixmpTelephone");
				hashMap.put("lineValue", ixmpTelephone);
				Membershiparents membershiparents2 = ixuestarParentsMapper.getMembershiparents(hashMap);
				if (membershiparents2==null) {
					String ixmpId = "ixmp" + new Date().getTime() + (int)(Math.random()*10);
					String ixmpCreatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
					membershiparents.setIxmpId(ixmpId);
					membershiparents.setIxmpTelephone(ixmpTelephone);
					membershiparents.setIxmpOpenid(ixmpOpenid);
					membershiparents.setIxmpCreatetime(ixmpCreatetime);
					ixuestarParentsMapper.writeMembershiparentsInfo(membershiparents);
					//然后写入关系表
					MemberAndParents memberAndParents = new MemberAndParents();
					String ixmapId = "ixmap" + new Date().getTime() + (int)(Math.random()*10);
					memberAndParents.setIxmapId(ixmapId);
					memberAndParents.setIxmpId(ixmpId);
					memberAndParents.setIxmId(ixmId);
					ixuestarParentsMapper.writeMemberAndParentsInfo(memberAndParents);
					//获取学生基本信息
					JSONArray jsonArray = new JSONArray();
					//获取会员学生信息关系表
					hashMap.put("lineKey", "ixmId");
					hashMap.put("lineValue", ixmId);
					MemberStudent memberStudent = ixuestarParentsMapper.getMemberStudent(hashMap);
					if (memberStudent!=null) {
						//获取学生是否为会员
						int imvType = 0;
						MembershipVipInfo membershipVipInfo = ixuestarParentsMapper.getMembershipVipInfo(hashMap);
						if (membershipVipInfo!=null) {
							imvType = membershipVipInfo.getImvType();
						}
						String ixssId = memberStudent.getIxssId();
						//获取学生信息
						hashMap.put("lineKey", "ixssId");
						hashMap.put("lineValue", ixssId);
						List<SchoolStudent> schoolStudents = ixuestarParentsMapper.getSchoolStudents(hashMap);
						for (int j = 0; j < schoolStudents.size(); j++) {
							SchoolStudent schoolStudent = schoolStudents.get(j);
							//添加
							JSONObject studentJson = (JSONObject)JSONObject.toJSON(schoolStudent);
							//获取班级信息
							String ixscId = schoolStudent.getIxscId();
							//获取班级信息
							hashMap.put("lineKey", "ixscId");
							hashMap.put("lineValue", ixscId);
							SchoolClass schoolClass = ixuestarParentsMapper.getSchoolClasses(hashMap).get(0);
							String ixsgId = schoolClass.getIxsgId();
							String ixscName = schoolClass.getIxscName();
							studentJson.put("ixscName", ixscName);							
							//获取学生id和年级名称
							hashMap.put("lineKey", "ixsgId");
							hashMap.put("lineValue", ixsgId);
							SchoolGrade schoolGrade = ixuestarParentsMapper.getSchoolGrades(hashMap).get(0);
							String ixsId = schoolGrade.getIxsId();
							String ixsgName = schoolGrade.getIxsgName();
							studentJson.put("ixsgId", ixsgId);	
							studentJson.put("ixsgName", ixsgName);
							hashMap.put("flag", 1);
							hashMap.put("lineKey", "ixsId");
							hashMap.put("lineValue", ixsId);
							String ixsName = ixuestarParentsMapper.getSchoolInfo(hashMap).get(0).getIxsName();
							studentJson.put("ixsName", ixsName);
							studentJson.put("imvType", imvType);
							jsonArray.add(studentJson);
						}
					}
					int jsonArraySize = jsonArray.size();
					jsObject.put("ixmpId", ixmpId);
					jsObject.put("studentInfo", jsonArray);
					jsObject.put("studentNumber", jsonArraySize);
				}else {
					code = 2;
				}
			}else {
				code = 1;
			}
			jsObject.put("code", code);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}

	@Override
	public JSONObject getSchoolInfos() {
		// TODO Auto-generated method stub
		JSONObject jsonObject = new JSONObject();
		try {
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("flag", 0);
			List<School> schoolInfo = ixuestarParentsMapper.getSchoolInfo(hashMap);
			int schoolNumber = schoolInfo.size();
			jsonObject.put("schoolList", schoolInfo);
			jsonObject.put("schoolNumber", schoolNumber);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsonObject;
	}

	@Override
	public JSONObject getSchoolGradeList(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		try {
			//接收参数
			String ixsId = jsonObject.getString("ixsId");
			//获取学校年级列表
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ixsId");
			hashMap.put("lineValue", ixsId);
			List<SchoolGrade> schoolGrades = ixuestarParentsMapper.getSchoolGrades(hashMap);
			int schoolGradeNumber = schoolGrades.size();
			jsObject.put("schoolGradesList", schoolGrades);
			jsObject.put("schoolGradeNumber", schoolGradeNumber);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}

	@Override
	public JSONObject getSchoolClassList(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		try {
			JSONArray jsonArray = new JSONArray();
			//首先接收参数
			String ixsgId = jsonObject.getString("ixsgId");
			//获取班级列表信息
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ixsgId");
			hashMap.put("lineValue", ixsgId);
			List<SchoolClass> schoolClasses = ixuestarParentsMapper.getSchoolClasses(hashMap);
			for (int i = 0; i < schoolClasses.size(); i++) {
				SchoolClass schoolClass = schoolClasses.get(i);
				String ixscNo = schoolClass.getIxscNo();
				if (!(ixscNo.substring(ixscNo.length()-2).equals("00"))) {
					jsonArray.add(schoolClass);
				}
			}
			int schoolClassNumber = jsonArray.size();
			jsObject.put("schoolClassesList", jsonArray);
			jsObject.put("schoolClassNumber", schoolClassNumber);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}

	@Override
	public JSONObject getSchoolStudentList(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		try {
			JSONArray jsonArray = new JSONArray();
			//首先接收参数
			String ixscId = jsonObject.getString("ixscId");
			//获取班级学生信息
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ixscId");
			hashMap.put("lineValue", ixscId);
			List<SchoolStudent> schoolStudents = ixuestarParentsMapper.getSchoolStudents(hashMap);
			for (int i = 0; i < schoolStudents.size(); i++) {
				SchoolStudent schoolStudent = schoolStudents.get(i);
				String ixssId = schoolStudent.getIxssId();
				//获取会员绑定信息
				hashMap.put("lineKey", "ixssId");
				hashMap.put("lineValue", ixssId);
				MemberStudent memberStudent = ixuestarParentsMapper.getMemberStudent(hashMap);
				if (memberStudent!=null) {
					String ixmId = memberStudent.getIxmId();
					//获取会员卡号
					hashMap.put("lineKey", "ixmId");
					hashMap.put("lineValue", ixmId);
					String ixmNo = ixuestarParentsMapper.getMembershipInfo(hashMap).getIxmNo();
					JSONObject stuJson = (JSONObject)JSONObject.toJSON(schoolStudent);
					stuJson.put("ixmNo", ixmNo);
					jsonArray.add(stuJson);
				}
			}
			int schoolStudentsNumber = jsonArray.size();
			jsObject.put("schoolStudentsList", jsonArray);
			jsObject.put("schoolStudentsNumber", schoolStudentsNumber);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
	
	@Override
	public JSONObject getSchoolStudentInfo(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		int code = 0;
		try {
			String ixmpOpenid = jsonObject.getString("ixmpOpenid");
			String ixmpTelephone = jsonObject.getString("ixmpTelephone");
			//获取家长信息
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ixmpTelephone");
			hashMap.put("lineValue", ixmpTelephone);
			Membershiparents membershiparents = ixuestarParentsMapper.getMembershiparents(hashMap);
			if (membershiparents!=null) {
				String ixmpId = membershiparents.getIxmpId();
				String ixmpOpenid2 = membershiparents.getIxmpOpenid();
				if (ixmpOpenid2.equals("wx3ebb778800722a19")) {
					membershiparents.setIxmpOpenid(ixmpOpenid);
					ixuestarParentsMapper.updateIxmpOpenId(membershiparents);
				}
				//获取关系信息表
				hashMap.put("lineKey", "ixmpId");
				hashMap.put("lineValue", ixmpId);
				List<MemberAndParents> memberAndParents = ixuestarParentsMapper.getMemberAndParents(hashMap);
				//创建json数组
				JSONArray jsonArray = new JSONArray();
				for (int i = 0; i < memberAndParents.size(); i++) {
					//获取会员信息id
					String ixmId = memberAndParents.get(i).getIxmId();
					//获取会员学生信息关系表
					hashMap.put("lineKey", "ixmId");
					hashMap.put("lineValue", ixmId);
					MemberStudent memberStudent = ixuestarParentsMapper.getMemberStudent(hashMap);
					if (memberStudent!=null) {
						//获取用户会员类型
						int imvType = 0;
						MembershipVipInfo membershipVipInfo = ixuestarParentsMapper.getMembershipVipInfo(hashMap);
						if (membershipVipInfo!=null) {
							imvType = membershipVipInfo.getImvType();
						}
						String ixssId = memberStudent.getIxssId();
						//获取会员卡号
						MembershipInfo membershipInfo = ixuestarParentsMapper.getMembershipInfo(hashMap);
						String ixmNo = membershipInfo.getIxmNo();
						//获取学生信息
						hashMap.put("lineKey", "ixssId");
						hashMap.put("lineValue", ixssId);
						List<SchoolStudent> schoolStudents = ixuestarParentsMapper.getSchoolStudents(hashMap);
						for (int j = 0; j < schoolStudents.size(); j++) {
							SchoolStudent schoolStudent = schoolStudents.get(j);
							//添加
							JSONObject studentJson = (JSONObject)JSONObject.toJSON(schoolStudent);
							//获取班级信息
							String ixscId = schoolStudent.getIxscId();
							//获取班级信息
							hashMap.put("lineKey", "ixscId");
							hashMap.put("lineValue", ixscId);
							SchoolClass schoolClass = ixuestarParentsMapper.getSchoolClasses(hashMap).get(0);
							String ixsgId = schoolClass.getIxsgId();
							String ixscName = schoolClass.getIxscName();
							studentJson.put("ixscName", ixscName);							
							//获取学生id和年级名称
							hashMap.put("lineKey", "ixsgId");
							hashMap.put("lineValue", ixsgId);
							SchoolGrade schoolGrade = ixuestarParentsMapper.getSchoolGrades(hashMap).get(0);
							String ixsId = schoolGrade.getIxsId();
							String ixsgName = schoolGrade.getIxsgName();
							studentJson.put("ixsgId", ixsgId);	
							studentJson.put("ixsgName", ixsgName);
							hashMap.put("flag", 1);
							hashMap.put("lineKey", "ixsId");
							hashMap.put("lineValue", ixsId);
							String ixsName = ixuestarParentsMapper.getSchoolInfo(hashMap).get(0).getIxsName();
							studentJson.put("ixsName", ixsName);
							studentJson.put("imvType", imvType);
							studentJson.put("ixmNo", ixmNo);
							jsonArray.add(studentJson);
						}
					}
				}
				int jsonArraySize = jsonArray.size();
				jsObject.put("ixmpId", ixmpId);
				jsObject.put("studentInfo", jsonArray);
				jsObject.put("studentNumber", jsonArraySize);
			}else {
				code = 1;
			}
			jsObject.put("code", code);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
	
	@Override
	public JSONObject getStudentInfoByIxmNo(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		int code = 0;
		try {
			String ixmNo = jsonObject.getString("ixmNo");
			//获取会员id
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ixmNo");
			hashMap.put("lineValue", ixmNo);
			MembershipInfo membershipInfo = ixuestarParentsMapper.getMembershipInfo(hashMap);
			if (membershipInfo!=null) {
				String ixmId = membershipInfo.getIxmId();
				//获取会员学生信息关系表
				hashMap.put("lineKey", "ixmId");
				hashMap.put("lineValue", ixmId);
				MemberStudent memberStudent = ixuestarParentsMapper.getMemberStudent(hashMap);
				if (memberStudent!=null) {
					String ixssId = memberStudent.getIxssId();
					//获取学生信息
					hashMap.put("lineKey", "ixssId");
					hashMap.put("lineValue", ixssId);
					List<SchoolStudent> schoolStudents = ixuestarParentsMapper.getSchoolStudents(hashMap);
					for (int j = 0; j < schoolStudents.size(); j++) {
						SchoolStudent schoolStudent = schoolStudents.get(j);
						//添加
						jsObject = (JSONObject)JSONObject.toJSON(schoolStudent);
						//获取班级信息
						String ixscId = schoolStudent.getIxscId();
						//获取班级信息
						hashMap.put("lineKey", "ixscId");
						hashMap.put("lineValue", ixscId);
						SchoolClass schoolClass = ixuestarParentsMapper.getSchoolClasses(hashMap).get(0);
						String ixsgId = schoolClass.getIxsgId();
						String ixscName = schoolClass.getIxscName();
						jsObject.put("ixscName", ixscName);							
						//获取学生id和年级名称
						hashMap.put("lineKey", "ixsgId");
						hashMap.put("lineValue", ixsgId);
						SchoolGrade schoolGrade = ixuestarParentsMapper.getSchoolGrades(hashMap).get(0);
						String ixsId = schoolGrade.getIxsId();
						String ixsgName = schoolGrade.getIxsgName();
						jsObject.put("ixsgName", ixsgName);
						hashMap.put("flag", 1);
						hashMap.put("lineKey", "ixsId");
						hashMap.put("lineValue", ixsId);
						String ixsName = ixuestarParentsMapper.getSchoolInfo(hashMap).get(0).getIxsName();
						jsObject.put("ixsName", ixsName);
					}
				}
			}else {
				code = 1;
			}
			jsObject.put("code", code);
		} catch (Exception e) {
			// TODO: handle exceptionjsObject
			e.printStackTrace();
		}
		return jsObject;
	}
	
	@Override
	public JSONObject getExaminationList(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		int code = 0;
		String ixsName = "总分";
		try {
			JSONArray jsonArray = new JSONArray();
			//接收参数
			String ixsgId = jsonObject.getString("ixsgId");
			String ixssId = jsonObject.getString("ixssId");
			//请求列表
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("flag", 0);
			hashMap.put("lineKey", "ixsgId");
			hashMap.put("lineValue", ixsgId);
			List<ExaminationInfo> examinationList = ixuestarParentsMapper.getExaminationList(hashMap);
			//添加json
			if (examinationList.size()!=0) {
				//获取考试成绩
				for (int i = 0; i < examinationList.size(); i++) {
					//获取对象
					ExaminationInfo examinationInfo = examinationList.get(i);
					String ixeId = examinationInfo.getIxeId();
					//获取科目信息
					hashMap.put("lineKey", "ixsName");
					hashMap.put("lineValue", ixsName);
					String ixsId = ixuestarParentsMapper.getSubjectInfo(hashMap).getIxsId();
					//获取考试科目的id
					String ixesId = "";
					hashMap.put("flag", 1);
					hashMap.put("lineKey", "ixsId");
					hashMap.put("lineValue", ixsId);
					hashMap.put("lineKey0", "ixeId");
					hashMap.put("lineValue0", ixeId);
					List<Examinationsubject> examinationsubjects = ixuestarParentsMapper.getExaminationsubject(hashMap);
					for (int j = 0; j < examinationsubjects.size(); j++) {
						Examinationsubject examinationsubject = examinationsubjects.get(j);
						ixesId = examinationsubject.getIxesId();
					}
					//获取考试总分
					String ixerFFenScore = "";
					hashMap.put("lineKey", "ixesId");
					hashMap.put("lineValue", ixesId);
					hashMap.put("lineKey0", "ixssId");
					hashMap.put("lineValue0", ixssId);
					List<ExaminationResults> examinationResults = ixuestarParentsMapper.getExaminationResults(hashMap);
					for (int k = 0; k < examinationResults.size(); k++) {
						ExaminationResults examinationResult = examinationResults.get(k);
						ixerFFenScore = examinationResult.getIxerFFenScore();
					}
					JSONObject jsObject = (JSONObject)JSONObject.toJSON(examinationInfo);
					jsObject.put("ixerFFenScore", ixerFFenScore);
					jsonArray.add(jsObject);
				}
				jsonObject.remove("ixsgId");
				jsonObject.put("examinationList", jsonArray);
				jsonObject.put("examinationNum", jsonArray.size());
			}else {
				code = 1;
			}
			jsonObject.put("code", code);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsonObject;
	}
	
	@Override
	public JSONObject getExaminationResultSubject(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		try {
			String ixeszId = "";
			String ixscId = "";
			String ixscAttribute = "";
			//接收参数
			String ixeId = jsonObject.getString("ixeId");
			String ixssId = jsonObject.getString("ixssId");
			//获取该学生的班级属性
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ixssId");
			hashMap.put("lineValue", ixssId);
			List<SchoolStudent> schoolStudents = ixuestarParentsMapper.getSchoolStudents(hashMap);
			for (int i = 0; i < schoolStudents.size(); i++) {
				ixscId = schoolStudents.get(i).getIxscId();
				hashMap.put("lineKey", "ixscId");
				hashMap.put("lineValue", ixscId);
				List<SchoolClass> schoolClasses = ixuestarParentsMapper.getSchoolClasses(hashMap);
				for (int j = 0; j < schoolClasses.size(); j++) {
					ixscAttribute = schoolClasses.get(j).getIxscAttribute();
				}
			}
			//获取该学生的考试科目
			JSONArray jsonArray = new JSONArray();
			hashMap.put("flag", 0);
			hashMap.put("lineKey", "ixeId");
			hashMap.put("lineValue", ixeId);
			List<Examinationsubject> examinationsubjects = ixuestarParentsMapper.getExaminationsubject(hashMap);
			for (int i = 0; i < examinationsubjects.size(); i++) {
				Examinationsubject examinationsubject = examinationsubjects.get(i);
				String ixsId = examinationsubject.getIxsId();
				String ixesId = examinationsubject.getIxesId();
				hashMap.put("lineKey", "ixsId");
				hashMap.put("lineValue", ixsId);
				String ixsName = ixuestarParentsMapper.getSubjectInfo(hashMap).getIxsName();
				String ixsFullMarks = ixuestarParentsMapper.getSubjectInfo(hashMap).getIxsFullMarks();
				//创建jsonObject
				JSONObject jsonObject1 = new JSONObject();
				jsonObject1.put("ixesId", ixesId);
				jsonObject1.put("ixsName", ixsName);
				jsonObject1.put("ixsFullMarks", ixsFullMarks);
				if (ixsName.equals("语文") || ixsName.equals("数学") || ixsName.equals("英语")  || ixsName.equals("总分") || ixscAttribute.contains(ixsName.substring(0, 1))) {
					jsonArray.add(jsonObject1);
					if (ixsName.equals("总分")) {
						ixeszId = ixesId;
					}
				}
			}
			//获取学生的考试成绩
			JSONArray jsonArray2 = new JSONArray();
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jsonObject2 = jsonArray.getJSONObject(i);
				String ixesId = jsonObject2.getString("ixesId");
				//请求成绩
				hashMap.put("flag", 1);
				hashMap.put("lineKey", "ixesId");
				hashMap.put("lineValue", ixesId);
				hashMap.put("lineKey0", "ixssId");
				hashMap.put("lineValue0", ixssId);
				List<ExaminationResults> examinationResults = ixuestarParentsMapper.getExaminationResults(hashMap);
				for (int j = 0; j < examinationResults.size(); j++) {
					ExaminationResults examinationResult = examinationResults.get(j);
					jsonObject2.putAll((JSONObject)JSONObject.toJSON(examinationResult));
				}
				jsonArray2.add(jsonObject2);
			}
			//获取平均分
			hashMap.put("ixscId", ixscId);
			hashMap.put("ixesId", ixeszId);
			Temp gradeAverage = ixuestarParentsMapper.getGradeAverage(ixeszId);
			String gradeAverageA = String.format("%.2f", Double.parseDouble(gradeAverage.getA()));
			String gradeAverageB = gradeAverage.getB();
			Temp classAverage = ixuestarParentsMapper.getClassAverage(hashMap);
			String classAverageA = "";
			if (classAverage!=null) {
				classAverageA = String.format("%.2f", Double.parseDouble(classAverage.getA()));
			}
			JSONObject jsonObject2 = new JSONObject();
			jsonObject2.put("gradeAverage", gradeAverageA);
			jsonObject2.put("classAverage", classAverageA);
			//添加返回
			jsObject.put("examStu", gradeAverageB);
			jsObject.put("ixesId", ixeszId);
			jsObject.put("resultInfo", jsonArray2);
			jsObject.put("average", jsonObject2);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
	
	@Override
	public JSONObject getExaminationResultCompare(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		int code = 0;
		try {
			//首先获取本次考试的ixeId和学生信息ixssId
			String ixeId = jsonObject.getString("ixeId");
			String ixssId = jsonObject.getString("ixssId");
//			String ixsgId = jsonObject.getString("ixsgId");
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("flag", 0);
			hashMap.put("lineKey", "ixeId");
			hashMap.put("lineValue", ixeId);
			String ixsgId = ixuestarParentsMapper.getExaminationList(hashMap).get(0).getIxsgId();
			//首先获取考试列表
			hashMap.put("flag", 0);
			hashMap.put("lineKey", "ixeId");
			hashMap.put("lineValue", ixeId);
			JSONArray jsonArray = new JSONArray();
			List<Examinationsubject> examinationsubjects = ixuestarParentsMapper.getExaminationsubject(hashMap);
			for (int i = 0; i < examinationsubjects.size(); i++) {
				//通过ixesId和ixssId获取学生的考试成绩放到数组里
				JSONObject jsonObject2 = new JSONObject();
				Examinationsubject examinationsubject = examinationsubjects.get(i);
				String ixesId = examinationsubject.getIxesId();
				String ixsId = examinationsubject.getIxsId();
				//获取科目名字
				hashMap.put("lineKey", "ixsId");
				hashMap.put("lineValue", ixsId);
				Subject subject = ixuestarParentsMapper.getSubjectInfo(hashMap);
				String ixsName = subject.getIxsName();
//				if (!ixsName.equals("总分")) {
					String ixsFullMarks = subject.getIxsFullMarks();
					//获取成绩
					String ixerRawScore = "";
					hashMap.put("flag", 1);
					hashMap.put("lineKey", "ixesId");
					hashMap.put("lineValue", ixesId);
					hashMap.put("lineKey0", "ixssId");
					hashMap.put("lineValue0", ixssId);
					List<ExaminationResults> examinationResults = ixuestarParentsMapper.getExaminationResults(hashMap);
					for (int j = 0; j < examinationResults.size(); j++) {
						if (ixsName.equals("总分")) {
							ixerRawScore = examinationResults.get(j).getIxerFFenScore();
						}else {
							ixerRawScore = examinationResults.get(j).getIxerRawScore();							
						}
						jsonObject2.put("ixesId", ixesId);
						jsonObject2.put("ixsName", ixsName);
						jsonObject2.put("ixsFullMarks", ixsFullMarks);
						jsonObject2.put("ixerRawScore", ixerRawScore);
						jsonArray.add(jsonObject2);
					}
//				}
			}
//			if (jsonArray.size()!=0) {
				JSONObject jsonObject1 = new JSONObject();
				jsonObject1.put("ixeId", ixeId);
				jsonObject1.put("score", jsonArray);
				//获取上一次的考试成绩
				JSONArray jsonArray1 = new JSONArray();
				String ixeId2 = "";
				hashMap.put("flag", 1);
				hashMap.put("lineKey", "ixsgId");
				hashMap.put("lineValue", ixsgId);
				List<ExaminationInfo> examinationLists = ixuestarParentsMapper.getExaminationList(hashMap);
				for (int i = 0; i < examinationLists.size(); i++) {
					ExaminationInfo examinationInfo = examinationLists.get(i);
					ixeId2 = examinationInfo.getIxeId();
					if (ixeId.equals(ixeId2)) {
						
					}else {
						hashMap.put("flag", 0);
						hashMap.put("lineKey", "ixeId");
						hashMap.put("lineValue", ixeId2);
						List<Examinationsubject> examinationsubjects1 = ixuestarParentsMapper.getExaminationsubject(hashMap);
						for (int j = 0; j < examinationsubjects1.size(); j++) {
							//通过ixesId和ixssId获取学生的考试成绩放到数组里
							JSONObject jsonObject2 = new JSONObject();
							Examinationsubject examinationsubject = examinationsubjects1.get(j);
							String ixesId = examinationsubject.getIxesId();
							String ixsId = examinationsubject.getIxsId();
							//获取科目名字
							hashMap.put("lineKey", "ixsId");
							hashMap.put("lineValue", ixsId);
							Subject subject = ixuestarParentsMapper.getSubjectInfo(hashMap);
							String ixsName = subject.getIxsName();
//							if (!ixsName.equals("总分")) {
								String ixsFullMarks = subject.getIxsFullMarks();
								//获取成绩
								String ixerRawScore = "";
								hashMap.put("flag", 1);
								hashMap.put("lineKey", "ixesId");
								hashMap.put("lineValue", ixesId);
								hashMap.put("lineKey0", "ixssId");
								hashMap.put("lineValue0", ixssId);
								List<ExaminationResults> examinationResults = ixuestarParentsMapper.getExaminationResults(hashMap);
								for (int k = 0; k < examinationResults.size(); k++) {
									if (ixsName.equals("总分")) {
										ixerRawScore = examinationResults.get(k).getIxerFFenScore();
									}else {
										ixerRawScore = examinationResults.get(k).getIxerRawScore();									
									}
									jsonObject2.put("ixesId", ixesId);
									jsonObject2.put("ixsName", ixsName);
									jsonObject2.put("ixsFullMarks", ixsFullMarks);
									jsonObject2.put("ixerRawScore", ixerRawScore);
									jsonArray1.add(jsonObject2);
								}
//							}
						}
					}
				}
//				if (jsonArray1.size()!=0) {
					JSONObject jsonObject2 = new JSONObject();
					jsonObject2.put("ixeId", ixeId2);
					jsonObject2.put("score", jsonArray1);
					//添加到jsonObject
					jsObject.put("thisExamination", jsonObject1);
					jsObject.put("LastExamination", jsonObject2);
//				}else {
//					code = 2;
//				}
//			}else {
//				code = 1;
//			}
			jsObject.put("code", code);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
	
	@Override
	public JSONObject getExaminationSubjectDetail(JSONObject jsonObject) {
		// TODO Auto-generated method stub\
		JSONObject jsObject = new JSONObject();
		try {
			//获取参数
			String ixesId = jsonObject.getString("ixesId");
			String ixssId = jsonObject.getString("ixssId");
			//获取考试分数
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("flag", 1);
			hashMap.put("lineKey", "ixesId");
			hashMap.put("lineValue", ixesId);
			hashMap.put("lineKey0", "ixssId");
			hashMap.put("lineValue0", ixssId);
			List<ExaminationResults> examinationResults = ixuestarParentsMapper.getExaminationResults(hashMap);
			//创建对象
			String ixerRawScore = "";
			JSONObject jsonObject2 = new JSONObject();
			for (int i = 0; i < examinationResults.size(); i++) {
				ExaminationResults examinationResult = examinationResults.get(i);
				ixerRawScore = examinationResult.getIxerRawScore();
				jsonObject2.put("ixerRawScore", ixerRawScore);
				String ixerFFenScore = examinationResult.getIxerFFenScore();
				jsonObject2.put("ixerFFenScore", ixerFFenScore);
				String ixerClassRanking = examinationResult.getIxerClassRanking();
				jsonObject2.put("ixerClassRanking", ixerClassRanking);
				String ixerSchoolRanking = examinationResult.getIxerSchoolRanking();
				jsonObject2.put("ixerSchoolRanking", ixerSchoolRanking);
			}
			//获取平均分以及总分
			hashMap.put("flag", 0);
			List<Examinationsubject> examinationsubject = ixuestarParentsMapper.getExaminationsubject(hashMap);
			JSONObject jsonObject3 = new JSONObject();
			jsonObject3.put("ixerRawScore", ixerRawScore);
			for (int i = 0; i < examinationsubject.size(); i++) {
				String ixsId = examinationsubject.get(i).getIxsId();
				//获取科目信息
				hashMap.put("lineKey", "ixsId");
				hashMap.put("lineValue", ixsId);
				String ixsFullMarks = ixuestarParentsMapper.getSubjectInfo(hashMap).getIxsFullMarks();
				jsonObject3.put("ixsFullMarks", ixsFullMarks);
			}
			//年级
			Temp gradeAverage = ixuestarParentsMapper.getGradeAverage(ixesId);
			String a = String.format("%.2f", Double.parseDouble(gradeAverage.getA()));
			jsonObject3.put("gradeAverage", a);
			String b = gradeAverage.getB();
			//班级
			hashMap.put("lineKey", "ixssId");
			hashMap.put("lineValue", ixssId);
			String ixscId = ixuestarParentsMapper.getSchoolStudents(hashMap).get(0).getIxscId();
			hashMap.put("ixscId", ixscId);
			hashMap.put("ixesId", ixesId);
			Temp classAverage = ixuestarParentsMapper.getClassAverage(hashMap);
			String a2 = String.format("%.2f", Double.parseDouble(classAverage.getA()));
			jsonObject3.put("classAverage", a2);
			//获取科目的题目列表
			hashMap.put("lineKey", "ixesId");
			hashMap.put("lineValue", ixesId);
			JSONArray jsonArray = new JSONArray();
			List<ExaminationQuestions> examinationQuestions = ixuestarParentsMapper.getExaminationQuestions(hashMap);
			for (int i = 0; i < examinationQuestions.size(); i++) {
				ExaminationQuestions examinationQuestion = examinationQuestions.get(i);
				jsonArray.add(examinationQuestion);	
			}
			//加入对象	
			jsObject.put("scoreDisplay", jsonObject3);
			jsObject.put("scoretable", jsonObject2);
			jsObject.put("questionList", jsonArray);
			jsObject.put("examStudent", b);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return jsObject;
	}
	
	@Override
	public JSONObject getExaminationQuestionInfo(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject quesObject = null;
		try {
			String ixesId = jsonObject.getString("ixesId");
			String ixssId = jsonObject.getString("ixssId");
			String ixeqId = jsonObject.getString("ixeqId");
			//获取详细情况
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("ixesId", ixesId);
			hashMap.put("ixeqId", ixeqId);
			hashMap.put("ixssId", ixssId);
			QuestionScore questionScores = ixuestarParentsMapper.getQuestionScores(hashMap);
			if (questionScores!=null) {
				//获取平均分
				hashMap.put("flag", 0);
				Temp questionAvgScores = ixuestarParentsMapper.getQuestionAvgScores(hashMap);
				String gradeA = "";
				if (questionAvgScores!=null) {
					gradeA = String.format("%.2f", Double.parseDouble(questionAvgScores.getA()));
				}
				//获取班级平均分
				hashMap.put("lineKey", "ixssId");
				hashMap.put("lineValue", ixssId);
				String ixscId = ixuestarParentsMapper.getSchoolStudents(hashMap).get(0).getIxscId();
				hashMap.put("flag", 1);
				hashMap.put("ixscId", ixscId);
				Temp questionAvgScores0 = ixuestarParentsMapper.getQuestionAvgScores(hashMap);
				String classA = "";
				if (questionAvgScores0!=null) {
					classA = String.format("%.2f", Double.parseDouble(questionAvgScores0.getA()));
				}
				//添加到对象
				quesObject = (JSONObject)JSONObject.toJSON(questionScores);
				quesObject.put("classA", classA);
				quesObject.put("gradeA", gradeA);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return quesObject;
	}
}
